/* $Id: ClientServiceRoleDAOImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.dao.provider;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import ae.pcfc.etks.online.common.dao.ClientServiceRoleDAO;
import ae.pcfc.etks.online.common.domain.ClientServiceRole;
import ae.pcfc.etks.online.common.exception.PersistenceException;

@Repository
public class ClientServiceRoleDAOImpl extends BaseDAO implements ClientServiceRoleDAO{
	
	
	/**
	 * add
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientServiceRoleDAO#add(ae.pcfc.etks.online.common.domain.ClientServiceRole)
	 * 
	 */
	public void add(ClientServiceRole clientServiceRole) {
		getHibernateTemplate().getSessionFactory().getCurrentSession().save(clientServiceRole);
		
	}

	/**
	 * findClientServiceRole
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientServiceRoleDAO#findClientServiceRole(ae.pcfc.etks.online.common.domain.ClientServiceRole)
	 * 
	 */
	public List<ClientServiceRole> findClientServiceRole(ClientServiceRole clientServiceRole) {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(ClientServiceRole.class, "csr");
		if(clientServiceRole.getClient() != null && clientServiceRole.getServiceRole() != null){
			criteria.createAlias("csr.client", "client");
			criteria.add(Restrictions.eq("client.CId", clientServiceRole.getClient().getCId()));
			criteria.createAlias("csr.serviceRole", "serviceRole");
			criteria.add(Restrictions.eq("serviceRole.srId", clientServiceRole.getServiceRole().getSrId()));
			@SuppressWarnings("unchecked")
			List<ClientServiceRole> list = criteria.list();
			if (list != null && list.size() > 0)
				return list;
			else
				return null;
		}else{
			return null;
		}
	}

	public <T> void delete(T obj) throws PersistenceException {
		getHibernateTemplate().getSessionFactory().getCurrentSession().delete(obj);
	}

}
